<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceEventNameList extends core {

    public function process() {
        $limit = $this->limit;
        $page = intval($_POST['page']);
        $user_id = intval($_POST['user_id']);
        if ($page < 1) {
            $page = 1;
        }
        $from = ($page - 1) * $limit;
        $total = 0;

        $data = $this->db->get_results("SELECT * FROM events WHERE user_id={$user_id} ORDER BY start_time asc LIMIT $from,$limit;");
        $total = $this->db->get_row("SELECT count(event_id) as total FROM events WHERE user_id={$user_id} ;");

        if (ceil($total->total / $limit) <= $page) {
            $end_paging = 1;
        } else {
            $end_paging = 0;
        }

        $html = '';
        foreach ($data as $event) {
            if ($event->start_time == "0000-00-00 00:00:00") {
                $html.='<li class="select_event" rel="' . $event->event_id . '" rev="' . $event->title . '">
                        <span>' . $event->title . '</span>
                    </li>';
            } else {
                $html.='<li class="select_event" rel="' . $event->event_id . '" >
                        <span>' . $event->title . '</span>
                    </li>';
            }
        }
        if ($html == '') {
            $html = '<li class="select_event">
                        <span>No events found.</span>
                    </li>';
        }
        $this->_JSONResponse(array('error' => 0, 'html' => $html, 'page' => $page + 1, 'end_paging' => $end_paging));
    }

}

$processor = new webServiceEventNameList();
$processor->process();
?>